﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using COMOSOFT.Class;
using MSoft.Shipping.Control;
using DevExpress.XtraGrid.Views.Grid;

namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class FrmBAGaneralView : Form
    {
        Task tak;
        int year;
        FrmBookMA frmBMA = null;
        object month, items;
        string Items = null;
        bool isClick = false;
        int NQty, EQty;

        public FrmBAGaneralView(int _Year,object _Month, object _Items)
        {
            InitializeComponent();
            this.year = _Year;
            this.month = _Month;
            this.items = _Items;
        }
        public void LoadData()
        {
            try
            {
                if (tak == null || tak.Status != TaskStatus.Running)
                {
                    progressPanel_BA.Show();
                    progressPanel_BA.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width / 2 - progressPanel_BA.Width, Screen.PrimaryScreen.WorkingArea.Height / 2 - progressPanel_BA.Height);
                    gridControl_BAGeneralView.DataSource = null;
                    tak = Task.Factory.StartNew(() => sp_BAGaneralViewTableAdapter.Fill(dataSetBalancing.sp_BAGaneralView, year, month, items));
                    tak.ContinueWith(tt => FillTable());
                }
                bandedGridView_Balance.ColumnPanelRowHeight = 30;
                new BandedCellSelectionHelper(bandedGridView_Balance);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }
        private delegate void dlgFillTable();
        private void FillTable()
        {
            try
            {
                if (InvokeRequired)
                {
                    this.Invoke(new dlgFillTable(FillTable));
                }
                else
                {
                    gridControl_BAGeneralView.DataSource = dataSetBalancing.sp_BAGaneralView;
                    progressPanel_BA.Hide();
                }
            }
            catch
            {
                MessageBox.Show("Progess fail");
            }
        }

        private void FrmBAGaneralView_Load(object sender, EventArgs e)
        {
        }

        private void bandedGridView_Balance_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            try
            {
                GridView view = sender as GridView;
                Items = null;
                if (e.FocusedRowHandle >= 0)
                {
                    if (!view.IsGroupRow(view.FocusedRowHandle))
                        Items = view.GetDataRow(e.FocusedRowHandle)["MALDIDs"].ToString();
                    sp_BanlanceDetailsTableAdapter.Fill(dataSetBalancing.sp_BanlanceDetails, Items, Convert.ToInt32(view.GetFocusedDataRow()["MADID"]));
                    dockPanel_BalanceDetails.Visibility = DevExpress.XtraBars.Docking.DockVisibility.Visible;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridControl_BAGeneralView_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (isClick==true)
                  radialMenu_BookMA.ShowPopup(Control.MousePosition, true);
            }
        }

        private void bnt_BookMA_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                radialMenu_BookMA.Collapse(false);
                switch (e.Item.Caption)
                {
                    case "Book MA":
                        if (gridView_BalanceDetails.Columns["NeedQty"].SummaryItem.SummaryValue.ToString() != "")
                            NQty = Convert.ToInt32(gridView_BalanceDetails.Columns["NeedQty"].SummaryItem.SummaryValue);
                        else
                            NQty = 0;
                        if (gridView_BalanceDetails.Columns["ExptQty"].SummaryItem.SummaryValue.ToString() != "")
                            EQty = Convert.ToInt32(gridView_BalanceDetails.Columns["ExptQty"].SummaryItem.SummaryValue);
                        else
                            EQty = 0;
                        int MAID = Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["MAID"]);
                        int MADID = Convert.ToInt32(bandedGridView_Balance.GetFocusedDataRow()["MADID"]);
                        string MALDIDs = bandedGridView_Balance.GetFocusedDataRow()["MALDIDs"].ToString();
                        frmBMA = new FrmBookMA(-1) { Text = "Add New Book Material" };
                        frmBMA.AddNew();
                        frmBMA.ShowControl();
                        frmBMA.SetValue(MAID, MADID, NQty - EQty, MALDIDs);
                        frmBMA.ShowDialog();                        
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bandedGridView_Balance_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "Color")
                isClick = true;
            else
                isClick = false;
        }
    }
}
